<template>
  <div class="fenlei">
    <div :class="hidd ? 'from' : 'from-active'">
      <el-button @click="setFlag">添加新优惠券</el-button>
      <div style="margin-top: 20px;width: 50%;">
        <el-table :data="tableData" border style="width: 100%">
          <el-table-column prop="id" label="ID"></el-table-column>
          <el-table-column prop="name" label="标题"></el-table-column>
          <el-table-column label="操作">
            <template #default="scope">
              <div style="letter-spacing: 10px;">
                <i @click="put(scope.row)" class="el-icon-edit"></i>
                <i @click="dele(scope.row)" class="el-icon-delete"></i>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <div class="zz" v-show="flag">
      <div class="set">
        <!-- 头部 -->
        <div class="setTop">
          <p style="color: #fff;">{{ state ? '添加分类' : '编辑分类' }}</p>
          <i class="el-icon-close" style="font-size: 20px;font-weight: bold;color: #fff;" @click="setFlag"></i>
        </div>
        <div>
          <!-- 主体 -->
          <div class="content" style="width: 50%;display: flex;flex-wrap: wrap;">
            <div style="display: flex;">
              分类名称：<el-input placeholder="请输入分类名称" v-model="name"></el-input>
            </div>
            <div style="margin-top: 20px;">
              <el-button @click="add" type="primary">确认</el-button>
              <el-button @click="setFlag">取消</el-button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getSelectListDjm, addSelectListDjm ,putSelectListDjm,deleteSelectListDjm} from '../../utils/api'
export default {
  data() {
    return {
      hidd: true,
      tableData: [],
      flag: false,
      name: '',
      state: true,
      id:null
    }
  },
  created() {
    this.render()
  },
  methods: {
    changeHidd() {
      this.hidd = !this.hidd
    },
    setFlag() {
      this.name=''
      this.state = true
      this.flag = !this.flag
    },
    render() {
      getSelectListDjm().then(res => {
        console.log(res);
        this.tableData = res.data.data
      })
    },
    add() {
      if (this.state) {
        addSelectListDjm({ name: this.name }).then(res => {
          this.flag = !this.flag
          this.render()
        })
      } else {
        putSelectListDjm({id:this.id,name:this.name}).then(res=>{
          this.flag = !this.flag
          this.render()
        })
      }
    },
    dele(v){
      let id=v.id
      this.id=id
      deleteSelectListDjm(this.id).then(res=>{
        this.render()
      })
    },
    put(v) {
      let id=v.id
      let name=v.name
      this.name=name
      this.id=id
      this.state = false
      this.flag = !this.flag
    }
  }
}
</script>

<style scoped>
.fenlei {
  width: 100%;
}

.from {
  display: block;
}

.from-active {
  display: none;
}

.zz {
  z-index: 10000;
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background-color: rgba(0, 0, 0, 0.5);
}

.setTop {
  padding: 18px 22px;
  display: flex;
  justify-content: space-between;
  background-color: #0790F5;
}

.set {
  font-size: 14px;
  width: 50%;
  height: 400px;
  position: absolute;
  top: 50%;
  left: 60%;
  -webkit-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
  background: #ffffff;
  border-radius: 5px;
}

.content {
  width: 85%;
  margin: 40px auto;
  height: 100%;
}

.lin2 button {
  border: 1px solid rgba(187, 187, 187, 1);
  color: rgba(16, 16, 16, 1);
  font-size: 14px;
  background-color: #fff;
  border-radius: 5px;
  width: 50px;
  height: 30px;
}

.el-upload__tip {
  color: red;
}

.lin2 {
  display: flex;
  width: 40%;
  margin: 0 auto;
  justify-content: space-around;
}

.upload-demo {
  margin-left: 20px;
  margin-top: -6px;
}

.nameipt {
  width: 100px;
  border: none;
  padding: 10px;
  margin-left: 60px;
  margin-top: -10px;
}

.icon1 {
  width: 25px;
  margin-left: 40px;
  margin-top: -5px;
}

.lin1 {
  display: flex;
  margin-bottom: 30px;
}

.nr {
  width: 100%;
  height: 100%;
  font-size: 14px;
}

.icon {
  width: 30px;
}

.tab {
  padding: 20px 40px;
  display: flex;
}

.content {
  width: 85%;
  margin: 40px auto;
  height: 100%;
}

.tabbtn {
  margin-left: 10px;
  margin-right: 10px;
  border: none;
  color: #0790F5;
  font-weight: bold;
  background-color: #fff;
}

table {
  margin-top: 30px;
  border: 1px solid #777777;
  border-collapse: collapse;
  text-align: center;
  width: 100%;
  font-size: 14px;
}

tr {
  height: 50px;
}

td {
  width: 150px;
  border: 1px solid #777777;
}

th {
  height: 50px;
  line-height: 50px;
  font-weight: normal;
  background-color: #F6F6F6;
  border: 1px solid #777777;
}
</style>